<?php
class Dashboard extends CI_Controller{
    var $_admin_view = '';
    
    function  __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
        $this->config->load('artyproperty');
        $this->_admin_view = $this->config->item('admin_view').'wrapper';
    }

    function index(){
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = ''; $id = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
            $level = $this->session->userdata('level');
            $id = $this->session->userdata('user_id');
        }else {
            redirect(site_url(),'location');
        }

        $sql = "select * from user where user_id = ?";
        $query = $this->db->query($sql,$id);
        $agen = $query->row();
        $data_output['agen']['nama_depan'] = $agen->nama_depan;
        $data_output['agen']['nama_belakang'] = $agen->nama_belakang;
        $data_output['agen']['no_hp'] = $agen->no_hp;
        $data_output['agen']['pin_bb'] = $agen->pin_bb;

        $sql = "select * from user, alamat where user.user_id = ? and user.alamat_id = alamat.alamat_id";
        $query = $this->db->query($sql,$agen->user_id);
        $alamat_agen = $query->row();
        $data_output['agen']['alamat']['jalan'] = $alamat_agen->jalan;
        $data_output['agen']['alamat']['kota_kab'] = $alamat_agen->kota_kab;
        $data_output['agen']['alamat']['provinsi'] = $alamat_agen->kota_kab;

        $sql = "select * from media where media.user_id = ?";
        $query = $this->db->query($sql,$agen->user_id);
        $data_output['agen']['picture'] = "";
        if($query->num_rows()==1){
            $pic_agen = $query->row();
            $data_output['agen']['picture'] = $pic_agen->url;
        }

        $sql = "select * from properti, user where user.user_id = properti.user_id";
        $query = $this->db->query($sql);
        $data_output['agen']['jml_listing'] = $query->num_rows();

        $sql = "select * from user, perusahaan where user.perusahaan_id = perusahaan.perusahaan_id 
                and user.user_id = ?";
        $query = $this->db->query($sql,$agen->user_id);
        $perusahaan = $query->row();
        $data_output['perusahaan']['nama'] = $perusahaan->nama_perusahaan;
        $data_output['perusahaan']['no_telp'] = $perusahaan->telp_kantor;
        $data_output['perusahaan']['fax'] = $perusahaan->fax;

        $sql = "select * from perusahaan, alamat where perusahaan.perusahaan_id = ? and perusahaan.alamat_id = alamat.alamat_id";
        $query = $this->db->query($sql,$perusahaan->perusahaan_id);
        $alamat_perusahaan = $query->row();
        $data_output['perusahaan']['alamat']['jalan'] = $alamat_perusahaan->jalan;
        $data_output['perusahaan']['alamat']['kota_kab'] = $alamat_perusahaan->kota_kab;
        $data_output['perusahaan']['alamat']['provinsi'] = $alamat_perusahaan->provinsi;
        $data_output['perusahaan']['alamat']['longitude'] = $alamat_perusahaan->longitude;
        $data_output['perusahaan']['alamat']['latitude'] = $alamat_perusahaan->latitude;

        $sql = "select * from media where media.perusahaan_id = ?";
        $query = $this->db->query($sql,$perusahaan->perusahaan_id);
        $data_output['perusahaan']['picture'] = "";
        if($query->num_rows() == 1){
            $pic_perusahaan = $query->row();
            $data_output['perusahaan']['picture'] = $pic_perusahaan->url;
        }
        
        $data = array('page_title'=>'Dashboard',
                'output' => $data_output,
                'isLogin' => $this->session->userdata('isLogin'),
            'user' => $this->session->userdata('username'),
            'level_user' => $this->session->userdata('level'),
                        'page_content' => 'dashboard');
        $this->load->view($this->_admin_view,$data);
    }
}

?>
